namespace Plugins.Engine
{
    using System;
    using System.Collections.Generic;
    using System.Diagnostics.CodeAnalysis;
    using Plugins.Core.Interfaces;
    using Plugins.Engine.Internal;
    using Plugins.Interfaces;
    using Plugins.Library;

    [Serializable]
    public class PluginObjectManager : PluginServiceManager, IPluginObjectManager, IPluginServiceManager
    {
        public PluginObjectManager(IPluginHostManager manager)
            : base(manager)
        {
        }

        [SuppressMessage("Microsoft.Design", "CA1004")]
        public static IPluginObjectCollection<T> GetObjectCollection<T>(IPluginHostManager manager, bool allowCompatibleObjects)
        {
            if (manager != null)
            {
                Type type = typeof(T);
                PluginObjectCollection<T> collection = new PluginObjectCollection<T>();
                foreach (IPluginObject plugin in manager.ObjectManager.PluginService.GetObjects(type))
                {
                    if (plugin.IsActive)
                    {
                        IPluginObject<T> obj = plugin as IPluginObject<T>;
                        if (obj != null)
                        {
                            collection.Add(obj);
                        }
                        else
                        {
                            if (allowCompatibleObjects)
                            {
                                IPluginObjectInternal internalPlugin = plugin as IPluginObjectInternal;
                                if (internalPlugin != null)
                                {
                                    if (PluginTypeHelper.IsCompatibleType(type, internalPlugin.ObjectType))
                                    {
                                        collection.Add(internalPlugin);
                                    }
                                }
                            }
                        }
                    }
                }
                if (collection.Count > 0)
                {
                    return collection;
                }
            }
            return null;
        }

        [SuppressMessage("Microsoft.Design", "CA1004")]
        public static IPluginObjectCollection<T> GetObjectCollection<T>(IPluginHostManager manager)
        {
            return GetObjectCollection<T>(manager, false);
        }

        [SuppressMessage("Microsoft.Design", "CA1004")]
        public static IEnumerable<T> GetServiceCollection<T>(IPluginHostManager manager)
        {
            if (manager != null)
            {
                Type type = typeof(T);
                IEnumerable<IPluginService<T>> services = PluginServiceManager.GetServices<T>(manager.ObjectManager);
                if (services != null)
                {
                    foreach (IPluginService<T> service in services)
                    {
                        foreach (T obj in service.GetServices(type))
                        {
                            yield return obj;
                        }
                    }
                }
            }
        }

        public bool RegisterObject(Type type, IPluginObject obj)
        {
            if (type != null && obj != null)
            {
                return PluginService.Register(type, obj);
            }
            return false;
        }

        public bool UnregisterObject(Type type, IPluginObject obj)
        {
            if (type != null && obj != null)
            {
                return PluginService.Unregister(type, obj);
            }
            return false;
        }
    }
}

